﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Database;
using System.Web;

namespace Business.Utilities
{
    public static class SystemLog
    {
        static DataClassesDataContext objDB = new DataClassesDataContext();

        public static void Insert(tblSystemLog log)
        {
            try
            {
                objDB.tblSystemLogs.InsertOnSubmit(log);
                objDB.SubmitChanges();
            }
            catch (Exception ex)
            {
                SystemLog.Insert(ex);
            }

        }

        public static void Insert(string message)
        {
            try
            {
                tblSystemLog objLog = new tblSystemLog();
                if (HttpContext.Current.Session["UserID"] != null)
                {
                    objLog.AccountId = Convert.ToInt32(HttpContext.Current.Session["UserID"]);
                }
                objLog.Url = HttpContext.Current.Request.Url.ToString();
                objLog.DateOfLog = DateTime.Now;
                objLog.IpAddress = HttpContext.Current.Request.UserHostAddress;
                objLog.Message = message;

                objDB.tblSystemLogs.InsertOnSubmit(objLog);
                objDB.SubmitChanges();
            }
            catch (Exception ex)
            {
                SystemLog.Insert(ex);
            }

        }

        public static void Insert(Exception exParameter)
        {
            try
            {
                tblSystemLog objLog = new tblSystemLog();
                if (HttpContext.Current.Session["UserID"] != null)
                {
                    objLog.AccountId = Convert.ToInt32(HttpContext.Current.Session["UserID"]);
                }
                objLog.Url = HttpContext.Current.Request.Url.ToString();
                objLog.DateOfLog = DateTime.Now;
                objLog.IpAddress = HttpContext.Current.Request.UserHostAddress;
                objLog.Message = exParameter.Message + " - " + exParameter.StackTrace;
                objDB.tblSystemLogs.InsertOnSubmit(objLog);
                objDB.SubmitChanges();
            }
            catch (Exception ex)
            {
                SystemLog.Insert(ex);
            }

        }
    }
}
